<template>
  <div class="security-edit-page">
    <el-card>
      <div class="edit-title">新增保安</div>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px" class="security-form">
        <div class="form-row">
          <el-form-item label="保安姓名：" prop="securityName">
            <el-input v-model="form.securityName" placeholder="请输入保安姓名" />
          </el-form-item>
          <el-form-item label="联系电话：" prop="contactPhone">
            <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
          </el-form-item>
        </div>
        <div class="form-actions">
          <el-button type="primary" @click="handleSubmit">确定</el-button>
          <el-button @click="goBack">取消</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { addSecurity } from '@/api/security'
import type { SysSecurity } from '@/types/security'

const router = useRouter()
const formRef = ref()
const form = reactive<SysSecurity>({
  securityName: '',
  contactPhone: ''
})

const rules = {
  securityName: [{ required: true, message: '请输入保安姓名', trigger: 'blur' }],
  contactPhone: [
    { required: true, message: '请输入联系电话', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ]
}

function handleSubmit() {
  formRef.value.validate((valid: boolean) => {
    if (valid) {
      addSecurity(form).then(() => {
        ElMessage.success('新增成功')
        goBack()
      })
    }
  })
}

function goBack() {
  router.back()
}
</script>

<style scoped>
.security-edit-page {
  padding: 40px 24px;
}
.edit-title {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 24px;
}
.security-form {
  background: #fafbfc;
  padding: 24px 16px 8px 16px;
  border-radius: 8px;
}
.form-row {
  display: flex;
  gap: 24px;
}
.form-actions {
  margin-top: 24px;
  text-align: left;
}
</style> 